using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Azure.Domain.Services;
using Azure.Membership;
using Rhino.Mocks;
using Azure.Web.Controllers;
using Xunit;

namespace Azure.Tests.Web.Controllers
{
    public class LoginFacts : ControllerFacts<AccountController>
    {
        [Fact]
        public void LoginDisplaysLoginForm()
        {
            var loginView = (ViewResult)ClassUnderTest.LogOn();

            Assert.NotNull(loginView);
        }

        [Fact]
        public void LoginWillDisplayLoginFormIfNotAuthenticated()
        {
            Mock<IMembershipService>().Stub(x => x.ValidateUser("test", "foo")).Return(false);

            var result = (ViewResult)ClassUnderTest.LogOn("test", "foo", false, null);

            Assert.NotNull(result);
        }

        [Fact]
        public void LoginWillAuthenticateTheUser()
        {
            Mock<IMembershipService>().Stub(x => x.ValidateUser("test", "foo")).Return(true);

            var result = (RedirectToRouteResult) ClassUnderTest.LogOn("test", "foo", false, null);

            Assert.Equal(Admin.Index, result.RouteValues["action"]);
            Assert.Equal(Admin.Controller, result.RouteValues["controller"]);
        }

        [Fact]
        public void LoginWillRedirectToPreviousPageAfterAuthenticated()
        {
            Mock<IMembershipService>().Stub(x => x.ValidateUser("test", "foo")).Return(true);

            var result = (RedirectResult)ClassUnderTest.LogOn("test", "foo", false, "/admin/");
            
            Assert.Equal("/admin/", result.Url);
        }
    }
}